import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {NzMessageService} from 'ng-zorro-antd/message';
import {EmployeeService} from '../../service/employee.service';
import {EmployeeQueryForm} from '../../types/req/EmployeeQueryForm';
import {SharedModule} from '../shared.module';
import {Employee} from '../../types/resp/Employee';
import {Page} from '../../types/page';
import {NzModalService} from 'ng-zorro-antd/modal';
import {MODAL_WIDTH} from '../../const';
import {EnterpriseSelectorComponent} from '../enterprise/enterprise-selector.component';
import {DepartmentSelectorComponent} from '../department/department-selector.component';
import {PositionSelectorComponent} from '../position/position-selector.component';

@Component({
  selector: 'employee-selector',
  imports: [
    EnterpriseSelectorComponent,
    DepartmentSelectorComponent,
    PositionSelectorComponent,
    SharedModule
  ],
  templateUrl: './employee-selector.component.html',
  standalone: true
})
export class EmployeeSelectorComponent implements OnInit {
  searchForm!: FormGroup;
  searchLoading: boolean = false
  listOfData: Employee[] = [];
  total: number = 0
  isVisible: boolean = false
  title: string = '员工基本信息选择器(双击行选中)'
  modalWidth: string = MODAL_WIDTH

  @ViewChild(EnterpriseSelectorComponent, { static: false }) enterpriseSelectorComponent!: EnterpriseSelectorComponent;
  @ViewChild(DepartmentSelectorComponent, { static: false }) departmentSelectorComponent!: DepartmentSelectorComponent;
  @ViewChild(PositionSelectorComponent, { static: false }) positionSelectorComponent!: PositionSelectorComponent;

  @Output() employeeSelectedEvent = new EventEmitter<{}>();

  constructor(
    private fb: FormBuilder,
    private message: NzMessageService,
    private employeeService: EmployeeService,
    private modal: NzModalService
  ) {}

  ngOnInit(): void {
    this.searchForm = this.fb.group({
      enterpriseId: [0],
      enterpriseName: [''],
      employeeNumber: [''],
      name: [''],
      departmentId: [0],
      departmentName: [''],
      positionId: [0],
      positionName: [''],
    })
    this.onSearch()
  }

  onReset() {
    this.searchForm.reset()
  }

  onSearch() {
    try {
      this.searchLoading = true
      this.employeeService.find(new EmployeeQueryForm(this.searchForm.value)).subscribe({
        next: (resp) => {
          if (!resp || resp.code !== 1) {
            this.message.error(resp && resp.msg ? resp.msg : '操作异常')
            return
          }
          const page: Page<Employee> | undefined = resp.data
          if (page && page.list.length > 0) {
            this.listOfData = page.list
            this.total = page.total
          }
        },
        complete: () => {
          this.searchLoading = false
        }
      })
    } catch (e) {
      console.log('获取数据异常', e)
    }
  }

  onRowDblClick(row: Employee, event: MouseEvent) {
    this.isVisible = false
    this.employeeSelectedEvent.emit({
      employeeId: row.id,
      employeeName: row.name,
    });
  }

  onBack() {
    this.isVisible = false
  }

  display() {
    this.isVisible = true
    this.searchForm.reset();
  }

  findEnterprise() {
    this.enterpriseSelectorComponent.display()
  }

  handleEnterpriseSelectedEvent(selectedEnterprise: { enterpriseId?: number; enterpriseName?: string; }) {
    if (selectedEnterprise && 'enterpriseId' in selectedEnterprise) {
      this.searchForm.patchValue(selectedEnterprise);
    }
  }
  findDepartment() {
    this.departmentSelectorComponent.display()
  }

  handleDepartmentSelectedEvent(selectedDepartment: { departmentId?: number; departmentName?: string; }) {
    if (selectedDepartment && 'departmentId' in selectedDepartment) {
      this.searchForm.patchValue(selectedDepartment);
    }
  }
  findPosition() {
    this.positionSelectorComponent.display()
  }

  handlePositionSelectedEvent(selectedPosition: { positionId?: number; positionName?: string; }) {
    if (selectedPosition && 'positionId' in selectedPosition) {
      this.searchForm.patchValue(selectedPosition);
    }
  }
}
